home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ8609.ZIP / ELKINS.SEP < prev    next >
Text File  |  1986-09-30  |  12KB  |  446 lines

  1.  
  2.  
  3. These listing are available for downlaoding from co-author Mike 
  4. Elkins Fido BBS at (619)722-8724 which operates at 300 and 1200 
  5. baud, 8-bits, no partity, 1 stop-bit.
  6.  
  7. ---- LISTING 1 ---------------------------------------------------------------------------
  8.  
  9. /*
  10.    Erathosthenes Sieve Prime Number Program in C
  11. */
  12. #define TRUE 1
  13. #define FALSE 0
  14. #define SIZE 8190
  15. #define SIZEP1 8191
  16. char flags[SIZEP1];
  17. main()
  18.  {
  19.  int i, prime, j, count, loops;
  20.  
  21.  loops = 1;
  22.  while(loops <= 10)   /* Changed to 100 for second benchmark */
  23.   {
  24.   count = 0;
  25.   i = 0;
  26.   while(i <= SIZE)
  27.    {
  28.    flags[i] = TRUE;
  29.    i++;
  30.    }
  31.   i = 0;
  32.   while(i <= SIZE)
  33.    {
  34.    if(flags[i])
  35.     {
  36.     prime = i + i + 3;
  37.     j = i + prime;
  38.     while(j <= SIZE)
  39.      {
  40.      flags[j] = FALSE;
  41.      j = j + prime;
  42.      }
  43.     count++;
  44.     }
  45.    i++;
  46.    }
  47.   loops++;
  48.   }
  49.  printf("%d primes, %d loops\n", count, loops);
  50.  return;
  51.  }
  52.  
  53.  
  54. ----- LISTING 2 --------------------------------------------------------------------------
  55. /*
  56.  *      "DHRYSTONE" Benchmark Program
  57.  *
  58.  *      Compile:        cl dry.c  (Microsoft 3.0)
  59.  *      Defines:        Defines are provided for old C compiler's
  60.  *                      which don't have enums, and can't assign structures.
  61.  *                      The time(2) function is library dependant; One is
  62.  *                      provided for CI-C86.  Your compiler may be different.
  63.  *                      This is not required for Microsoft 3.0 which supports
  64.  *                      all of the standard calls and will compile asis.
  65.  *
  66.  *      MACHINE                 OPERATING       COMPILER         DHRYSTONES 
  67.  *      TYPE                    SYSTEM                            /SEC      
  68.  *      ---------------         ------------    ---------------  -----------
  69.  *      IBM PC                  PCDOS 3.1       Microsoft C 3.0      333
  70.  *      IBM PC/AT               PCDOS 3.1       Microsoft C 3.0     1041
  71.  *    $ IBM PC/AT               PCDOS 3.0       CI-C86 2.1           684
  72.  *    $ ATT 3B2/300             UNIX 5.2        cc                   806
  73.  *    $ IBM PC/AT               VENIX/86 2.1    cc                  1000
  74.  *    $ Sun2/120                Sun 4.2BSD      cc                  1219
  75.  *
  76.  *    The entries with $ supplied by Rick Richardson, who origanally converted
  77.  *    the program from ADA.  
  78.  *    The rest are provided by Mike's "C" Board 619-722-8724 .
  79.  **************************************************************************
  80.  *
  81.  *      The following program contains statements of a high-level programming
  82.  *      language (C) in a distribution considered representative:
  83.  *
  84.  *      assignments                     53%
  85.  *      control statements              32%
  86.  *      procedure, function calls       15%
  87.  *
  88.  *      100 statements are dynamically executed.  The program is balanced with
  89.  *      respect to the three aspects:
  90.  *              - statement type
  91.  *              - operand type (for simple data types)
  92.  *              - operand access
  93.  *                      operand global, local, parameter, or constant.
  94.  *
  95.  *      The combination of these three aspects is balanced only approximately.
  96.  *
  97.  *      The program does not compute anything meaningfull, but it is
  98.  *      syntactically and semantically correct.
  99.  *
  100.  */
  101.  
  102. /* Compiler dependent options */
  103. #undef  NOENUM                  /* Define if compiler has no enum's */
  104. #undef  NOSTRUCTASSIGN          /* Define if compiler can't assign structures */
  105. #undef  NOTIME                  /* Define if no time() function in library */
  106.  
  107. #ifdef  NOSTRUCTASSIGN
  108. #define structassign(d, s)      memcpy(&(d), &(s), sizeof(d))
  109. #else
  110. #define structassign(d, s)      d = s
  111. #endif
  112.  
  113. #ifdef  NOENUM
  114. #define Ident1  1
  115. #define Ident2  2
  116. #define Ident3  3
  117. #define Ident4  4
  118. #define Ident5  5
  119. typedef int     Enumeration;
  120. #else
  121. typedef enum    {Ident1, Ident2, Ident3, Ident4, Ident5} Enumeration;
  122. #endif
  123.  
  124. typedef int     OneToThirty;
  125. typedef int     OneToFifty;
  126. typedef char    CapitalLetter;
  127. typedef char    String30[31];
  128. typedef int     Array1Dim[51];
  129. typedef int     Array2Dim[51][51];
  130.  
  131. struct  Record
  132. {
  133.         struct Record           *PtrComp;
  134.         Enumeration             Discr;
  135.         Enumeration             EnumComp;
  136.         OneToFifty              IntComp;
  137.         String30                StringComp;
  138. };
  139.  
  140. typedef struct Record   RecordType;
  141. typedef RecordType *    RecordPtr;
  142. typedef int             boolean;
  143.  
  144. #define NULL            0
  145. #define TRUE            1
  146. #define FALSE           0
  147.  
  148. #ifndef REG
  149. #define REG
  150. #endif
  151.  
  152. extern Enumeration      Func1();
  153. extern boolean          Func2();
  154.  
  155. main()
  156. {
  157.         Proc0();
  158. }
  159.  
  160. /*
  161.  * Package 1
  162.  */
  163. int             IntGlob;
  164. boolean         BoolGlob;
  165. char            Char1Glob;
  166. char            Char2Glob;
  167. Array1Dim       Array1Glob;
  168. Array2Dim       Array2Glob;
  169. RecordPtr       PtrGlob;
  170. RecordPtr       PtrGlobNext;
  171.  
  172. Proc0()
  173. {
  174.         OneToFifty              IntLoc1;
  175.         REG OneToFifty          IntLoc2;
  176.         OneToFifty              IntLoc3;
  177.         REG char                CharLoc;
  178.         REG char                CharIndex;
  179.         REG Enumeration         EnumLoc;
  180.         String30                String1Loc;
  181.         String30                String2Loc;
  182.  
  183. #define LOOPS           50000
  184. long                    time();
  185. long                    starttime;
  186. long                    benchtime;
  187. long                    nulltime;
  188. register unsigned int   i;
  189. starttime = time(0);
  190. for (i = 0; i < LOOPS; ++i);
  191. nulltime = time(0) - starttime;
  192.  
  193.         PtrGlobNext = (RecordPtr) malloc(sizeof(RecordType));
  194.         PtrGlob = (RecordPtr) malloc(sizeof(RecordType));
  195.         PtrGlob->PtrComp = PtrGlobNext;
  196.         PtrGlob->Discr = Ident1;
  197.         PtrGlob->EnumComp = Ident3;
  198.         PtrGlob->IntComp = 40;
  199.         strcpy(PtrGlob->StringComp, "DHRYSTONE PROGRAM, SOME STRING");
  200.  
  201. /*****************
  202. -- Start Timer --
  203. *****************/
  204. starttime = time(0);
  205. for (i = 0; i < LOOPS; ++i)
  206. {
  207.  
  208.         Proc5();
  209.         Proc4();
  210.         IntLoc1 = 2;
  211.         IntLoc2 = 3;
  212.         strcpy(String2Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
  213.         EnumLoc = Ident2;
  214.         BoolGlob = ! Func2(String1Loc, String2Loc);
  215.         while (IntLoc1 < IntLoc2)
  216.         {
  217.                 IntLoc3 = 5 * IntLoc1 - IntLoc2;
  218.                 Proc7(IntLoc1, IntLoc2, &IntLoc3);
  219.                 ++IntLoc1;
  220.         }
  221.         Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3);
  222.         Proc1(PtrGlob);
  223.         for (CharIndex = 'A'; CharIndex <= Char2Glob; ++CharIndex)
  224.                 if (EnumLoc == Func1(CharIndex, 'C'))
  225.                         Proc6(Ident1, &EnumLoc);
  226.         IntLoc3 = IntLoc2 * IntLoc1;
  227.         IntLoc2 = IntLoc3 / IntLoc1;
  228.         IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1;
  229.         Proc2(&IntLoc1);
  230.  
  231. }
  232.  
  233. /*****************
  234. -- Stop Timer --
  235. *****************/
  236. benchtime = time(0) - starttime - nulltime;
  237. printf("Dhrystone time for %ld passes = %ld\n", (long) LOOPS, benchtime);
  238. printf("This machine benchmarks at %ld dhrystones/second\n",
  239.         ((long) LOOPS) / benchtime);
  240.  
  241. }
  242.  
  243. Proc1(PtrParIn)
  244. REG RecordPtr   PtrParIn;
  245. {
  246. #define NextRecord      (*(PtrParIn->PtrComp))
  247.  
  248.         structassign(NextRecord, *PtrGlob);
  249.         PtrParIn->IntComp = 5;
  250.         NextRecord.IntComp = PtrParIn->IntComp;
  251.         NextRecord.PtrComp = PtrParIn->PtrComp;
  252.         Proc3(NextRecord.PtrComp);
  253.         if (NextRecord.Discr == Ident1)
  254.         {
  255.                 NextRecord.IntComp = 6;
  256.                 Proc6(PtrParIn->EnumComp, &NextRecord.EnumComp);
  257.                 NextRecord.PtrComp = PtrGlob->PtrComp;
  258.                 Proc7(NextRecord.IntComp, 10, &NextRecord.IntComp);
  259.         }
  260.         else
  261.                 structassign(*PtrParIn, NextRecord);
  262.  
  263. #undef  NextRecord
  264. }
  265.  
  266. Proc2(IntParIO)
  267. OneToFifty      *IntParIO;
  268. {
  269.         REG OneToFifty          IntLoc;
  270.         REG Enumeration         EnumLoc;
  271.  
  272.         IntLoc = *IntParIO + 10;
  273.         for(;;)
  274.         {
  275.                 if (Char1Glob == 'A')
  276.                 {
  277.                         --IntLoc;
  278.                         *IntParIO = IntLoc - IntGlob;
  279.                         EnumLoc = Ident1;
  280.                 }
  281.                 if (EnumLoc == Ident1)
  282.                         break;
  283.         }
  284. }
  285.  
  286. Proc3(PtrParOut)
  287. RecordPtr       *PtrParOut;
  288. {
  289.         if (PtrGlob != NULL)
  290.                 *PtrParOut = PtrGlob->PtrComp;
  291.         else
  292.                 IntGlob = 100;
  293.         Proc7(10, IntGlob, &PtrGlob->IntComp);
  294. }
  295.  
  296. Proc4()
  297. {
  298.         REG boolean     BoolLoc;
  299.  
  300.         BoolLoc = Char1Glob == 'A';
  301.         BoolLoc |= BoolGlob;
  302.         Char2Glob = 'B';
  303. }
  304.  
  305. Proc5()
  306. {
  307.         Char1Glob = 'A';
  308.         BoolGlob = FALSE;
  309. }
  310.  
  311. extern boolean Func3();
  312.  
  313. Proc6(EnumParIn, EnumParOut)
  314. REG Enumeration EnumParIn;
  315. REG Enumeration *EnumParOut;
  316. {
  317.         *EnumParOut = EnumParIn;
  318.         if (! Func3(EnumParIn) )
  319.                 *EnumParOut = Ident4;
  320.         switch (EnumParIn)
  321.         {
  322.         case Ident1:    *EnumParOut = Ident1; break;
  323.         case Ident2:    if (IntGlob > 100) *EnumParOut = Ident1;
  324.                         else *EnumParOut = Ident4;
  325.                         break;
  326.         case Ident3:    *EnumParOut = Ident2; break;
  327.         case Ident4:    break;
  328.         case Ident5:    *EnumParOut = Ident3;
  329.         }
  330. }
  331.  
  332. Proc7(IntParI1, IntParI2, IntParOut)
  333. OneToFifty      IntParI1;
  334. OneToFifty      IntParI2;
  335. OneToFifty      *IntParOut;
  336. {
  337.         REG OneToFifty  IntLoc;
  338.  
  339.         IntLoc = IntParI1 + 2;
  340.         *IntParOut = IntParI2 + IntLoc;
  341. }
  342.  
  343. Proc8(Array1Par, Array2Par, IntParI1, IntParI2)
  344. Array1Dim       Array1Par;
  345. Array2Dim       Array2Par;
  346. OneToFifty      IntParI1;
  347. OneToFifty      IntParI2;
  348. {
  349.         REG OneToFifty  IntLoc;
  350.         REG OneToFifty  IntIndex;
  351.  
  352.         IntLoc = IntParI1 + 5;
  353.         Array1Par[IntLoc] = IntParI2;
  354.         Array1Par[IntLoc+1] = Array1Par[IntLoc];
  355.         Array1Par[IntLoc+30] = IntLoc;
  356.         for (IntIndex = IntLoc; IntIndex <= (IntLoc+1); ++IntIndex)
  357.                 Array2Par[IntLoc][IntIndex] = IntLoc;
  358.         ++Array2Par[IntLoc][IntLoc-1];
  359.         Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc];
  360.         IntGlob = 5;
  361. }
  362.  
  363. Enumeration Func1(CharPar1, CharPar2)
  364. CapitalLetter   CharPar1;
  365. CapitalLetter   CharPar2;
  366. {
  367.         REG CapitalLetter       CharLoc1;
  368.         REG CapitalLetter       CharLoc2;
  369.  
  370.         CharLoc1 = CharPar1;
  371.         CharLoc2 = CharLoc1;
  372.         if (CharLoc2 != CharPar2)
  373.                 return (Ident1);
  374.         else
  375.                 return (Ident2);
  376. }
  377.  
  378. boolean Func2(StrParI1, StrParI2)
  379. String30        StrParI1;
  380. String30        StrParI2;
  381. {
  382.         REG OneToThirty         IntLoc;
  383.         REG CapitalLetter       CharLoc;
  384.  
  385.         IntLoc = 1;
  386.         while (IntLoc <= 1)
  387.                 if (Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1)
  388.                 {
  389.                         CharLoc = 'A';
  390.                         ++IntLoc;
  391.                 }
  392.         if (CharLoc >= 'W' && CharLoc <= 'Z')
  393.                 IntLoc = 7;
  394.         if (CharLoc == 'X')
  395.                 return(TRUE);
  396.         else
  397.         {
  398.                 if (strcmp(StrParI1, StrParI2) > 0)
  399.                 {
  400.                         IntLoc += 7;
  401.                         return (TRUE);
  402.                 }
  403.                 else
  404.                         return (FALSE);
  405.         }
  406. }
  407.  
  408. boolean Func3(EnumParIn)
  409. REG Enumeration EnumParIn;
  410. {
  411.         REG Enumeration EnumLoc;
  412.  
  413.         EnumLoc = EnumParIn;
  414.         if (EnumLoc == Ident3) return (TRUE);
  415.         return (FALSE);
  416. }
  417.  
  418. #ifdef  NOSTRUCTASSIGN
  419. memcpy(d, s, l)
  420. register char   *d;
  421. register char   *s;
  422. int     l;
  423. {
  424.         while (l--) *d++ = *s++;
  425. }
  426. #endif
  427.  
  428. /*
  429.  *      Library function for compilers with no time(2) function in the
  430.  *      library.
  431.  */
  432. #ifdef  NOTIME
  433. long    time(p)
  434. long    *p;
  435. {               /* CI-C86 time function - don't use around midnight */
  436.         long    t;
  437.         struct regval {unsigned int ax,bx,cx,dx,si,di,ds,es; } regs;
  438.  
  439.         regs.ax = 0x2c00;
  440.         sysint21(®s, ®s);
  441.         t = ((regs.cx>>8)*60L + (regs.cx & 0xff))*60L + (regs.dx>>8);
  442.         if (p) *p = t;
  443.         return t;
  444. }
  445. #endif
  446.